﻿Imports System.Data.SqlClient

Public Class ConfigWindow

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim state As Boolean = False
        Dim errorText As String = ""
        Dim x As Integer = 0
        If Not (txtServerName.Text.Length > 0) Then
            errorText += "ServerName is required" & vbLf
            state = True
        End If
        If Not (txtDatabaseName.Text.Length > 0) Then
            errorText += "DataBase Name is required" & vbLf
            state = True
        End If

        If Not (txtUserName.Text.Length > 0) Then
            errorText += "UserName is required"
            state = True
        End If

        If state = False Then
            Try
                Dim conStr As String = ((("Data Source=" + txtServerName.Text & ";Initial Catalog=") + txtDatabaseName.Text & ";User Id=") + txtUserName.Text & ";Password=") + pbPassword.Text
                Settings.[Default].ConnectionStr = conStr
                Settings.[Default].Save()
                Settings.[Default].Reload()
                MessageBox.Show("Config has changed successfully. You need to launch the application again.")
                Me.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        Else
            MessageBox.Show(errorText, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Cursor = Cursors.WaitCursor
        Dim state As Boolean = False
        Dim errorText As String = ""
        Dim x As Integer = 0
        If Not (txtServerName.Text.Length > 0) Then
            errorText += "ServerName is required" & vbLf
            state = True
        End If
        If Not (txtDatabaseName.Text.Length > 0) Then
            errorText += "DataBase Name is required" & vbLf
            state = True
        End If

        If Not (txtUserName.Text.Length > 0) Then
            errorText += "UserName is required"
            state = True
        End If

        If state = False Then
            Dim conStr As String = ((("Data Source=" + txtServerName.Text & ";Initial Catalog=") + txtDatabaseName.Text & ";User Id=") + txtUserName.Text & ";Password=") + pbPassword.Text
            Dim con As New SqlConnection(conStr)
            Try
                con.Open()
                con.Close()
                MessageBox.Show(ErrorMessages.[Default].ConnectionTestSucceed)
            Catch generatedExceptionName As SqlException
                MessageBox.Show(ErrorMessages.[Default].ConnectionTestFailed, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])

            End Try
        Else
            MessageBox.Show(errorText, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End If
        Me.Cursor = Cursors.Arrow
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    End Sub

    Private Sub ConfigWindow_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim conStr As String = Settings.[Default].ConnectionStr
            Dim arrTemp As String() = conStr.Split(";"c)
            txtServerName.Text = arrTemp(0).Split("="c)(1)
            txtDatabaseName.Text = arrTemp(1).Split("="c)(1)

            txtUserName.Text = arrTemp(2).Split("="c)(1)
            pbPassword.Text = arrTemp(3).Split("="c)(1)
            'SliderTimeout.Value = Convert.ToDouble(arrTemp(4).Split("="c)(1))
        Catch generatedExceptionName As Exception
        End Try
    End Sub
End Class